package com.zcf.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.zcf.common.json.Body;
import com.zcf.common.result.PageResult;
import com.zcf.common.result.ResultVo;
import com.zcf.pojo.Coupon;
import com.zcf.pojo.Dining;
import com.zcf.pojo.MyCoupon;
import com.zcf.pojo.User;
import com.zcf.utils.DateUtil;
import com.zcf.utils.Hutool;
import com.zcf.vo.in.PageVo;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/coupon")
@CrossOrigin
@ResponseBody
public class CouponController {

	/**
	 * 查询平台优惠劵
	 */
	@PostMapping("queryCoupon")
	public Body queryCoupon(String startTime, String stopTime, String dinName) {
		EntityWrapper<Coupon> ewq = new EntityWrapper<>();
		Coupon coupon = new Coupon();
		if (startTime != null && startTime != "" && stopTime != null && stopTime != "") {
			ewq.between("createtime", startTime, stopTime);
		} else if (startTime != null && startTime != "") {
			ewq.like("createtime", startTime);
		} else if (stopTime != null && stopTime != "") {
			ewq.like("stop_time", stopTime);
		}
		ewq.eq("type", 2);
		List<Coupon> list = coupon.selectList(ewq);
		List<Coupon> lists = new ArrayList<>();
		for (Coupon coupon2 : list) {
			lists.add(coupon2);
		}
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.BODY_451;
	}

	// 设置通用红包
	@PostMapping("/toCoupon")
	public Body toCoupon(String name,String diningId, Double money, String stopTime, String createtime, Integer nums,Double total) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(date);
		Coupon coupon = new Coupon();
		coupon.setId(Hutool.getId());
		coupon.setMoney(money);
		coupon.setStopTime(stopTime);
		coupon.setType(2);
//		coupon.setDiningId(diningId);
		coupon.setCreatetime(createtime);
		coupon.setAddtime(time);
		if(name!=null){
			coupon.setName(name);

		}
		if(total!=null){
			coupon.setTotal(total);
		}else{

		}
		if(nums!=null){
			coupon.setNums(nums);
		}
		boolean insert = coupon.insert();
		if (insert) {
			return Body.newInstance(coupon);
		}
		return Body.newInstance(501, "上传有误");
	}

	// 设置满减红包
	@PostMapping("/manCoupon")
	public Body manCoupon(String diningId, Double money, Double total, String stoptime, String createtime,
			Integer type,Integer nums) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(date);
		Coupon coupon = new Coupon();
		coupon.setId(Hutool.getId());
		coupon.setMoney(money);
		coupon.setStopTime(stoptime);
		coupon.setDiningId(diningId);
		coupon.setTotal(total);
		coupon.setType(1);
		coupon.setCreatetime(createtime);
		coupon.setAddtime(time);

		coupon.setNums(nums);
		Dining dining = new Dining();
		dining = dining.selectById(diningId);
		if (dining == null) {

		}else {
			coupon.setName(dining.getDiningName()+"惊喜红包");
		}
		boolean insert = coupon.insert();
		if (insert) {
			return Body.newInstance(coupon);
		}
		return Body.newInstance(501, "上传有误");
	}

	/**
	 *
	 * @param userId
	 * @return
	 */
	@PostMapping("pingtai")
	public Body pingtai(String userId) {
		Coupon coupon = new Coupon();
		List<Coupon> list = coupon.selectAll();
		String time = Hutool.parseDateToStringLp();
		List<Coupon> lists = new ArrayList<>();
		for (Coupon coupon2 : list) {
			if (coupon2.getDiningId() == null && coupon2.getDiningId().equals("")) {
				continue;
			}
			// 获取开始时间距今时间
			String times = "";
			if (coupon2.getCreatetime() != null && coupon2.getCreatetime() != "") {
				times = DateUtil.dateDiff(coupon2.getCreatetime(), time, "yyyy-MM-dd", "d");
			}
			// 获取截止时间距今时间
			String dateTime = "";
			if (coupon2.getStopTime() != null) {
				dateTime = DateUtil.dateDiff(coupon2.getStopTime(), time, "yyyy-MM-dd", "d");
			}
			if (!dateTime.equals("0") || Integer.parseInt(dateTime) < 0 && Integer.parseInt(times) > 0) {
				EntityWrapper<MyCoupon> ews = new EntityWrapper<>();
				MyCoupon myCoupon = new MyCoupon();
				ews.eq("user_id", userId);
				ews.eq("coupon_id", coupon2.getId());
				int count = myCoupon.selectCount(ews);
				if (count > 0) {
					coupon2.setText(1);
				} else {
					coupon2.setText(0);
				}
				lists.add(coupon2);
			} else {
				coupon2.deleteById();
				continue;
			}
			continue;
		}

		if (!lists.isEmpty()) {
			return Body.newInstance(lists);
		}
		return Body.newInstance(501, "目前没有优惠劵");
	}

	// 查看商家优惠劵
	@PostMapping("/check")
	public Body check(Integer type, String dining_id, String userId) {
		Coupon coupon = new Coupon();
		EntityWrapper<Coupon> ew = new EntityWrapper<Coupon>();
		if (type != null) {
			ew.eq("type", type);
		}
		if (dining_id != null && dining_id != "") {
			ew.eq("dining_id", dining_id);
		}
		Dining dining = new Dining();
		ew.orderBy("stop_time", true);
		List<Coupon> list = coupon.selectList(ew);
		List<Coupon> lists = new ArrayList<>();
		String time = Hutool.parseDateToStringLp();
		for (Coupon coupon2 : list) {
			// 获取开始时间距今时间
			String times = "";
			if (coupon2.getCreatetime() != null && coupon2.getCreatetime() != "") {
				times = DateUtil.dateDiff(coupon2.getCreatetime(), time, "yyyy-MM-dd", "d");
			}
			// 获取截止时间距今时间
			String dateTime = "";
			if (coupon2.getStopTime() != null) {
				dateTime = DateUtil.dateDiff(coupon2.getStopTime(), time, "yyyy-MM-dd", "d");
			}
			if (!dateTime.equals("0") || Integer.parseInt(dateTime) < 0 && Integer.parseInt(times) > 0) {
				EntityWrapper<MyCoupon> ews = new EntityWrapper<>();
				MyCoupon myCoupon = new MyCoupon();
				ews.eq("user_id", userId);
				ews.eq("coupon_id", coupon2.getId());
				int count = myCoupon.selectCount(ews);
				if (count > 0) {
					coupon2.setText(1);
				} else {
					coupon2.setText(0);
				}

				Dining dining2 =  dining.selectById(coupon2.getDiningId());
				if (dining2 != null) {
					coupon2.setDinName(dining2.getDiningName());
				}
				lists.add(coupon2);

			} else {
				coupon2.deleteById();
				continue;
			}
			continue;
		}
		if (!lists.isEmpty()) {
			return Body.newInstance(lists);
		}
		return Body.newInstance(501, "目前没有优惠劵");
	}

	// 删除优惠劵
	@PostMapping("/delete")
	public Body delete(String id) {
		boolean b = false;
		String[] appid = id.split(",");
		for (int i = 0; i < appid.length; i++) {
			String string = appid[i];
			Coupon coupon = new Coupon();
			coupon.setId(string);
			b = coupon.deleteById();
		}
		if (b) {
			return Body.newInstance(200, "删除成功");
		}
		return Body.newInstance(501, "删除有误");
	}

	/**
	 * 查询优惠劵 新
	 *
	 * @param id 用户id counId 需要查询的优惠劵id
	 */
	@PostMapping("queryXin")
	public Body queryXin(String id, String counId, PageVo pv) {
		User user = new User();
		Dining dining =new Dining();
		user = user.selectById(id);
		if (user == null) {
			return Body.newInstance("用户不存在");
		}
		user.setCount(user.getCount() + 1);
		Boolean falg = user.updateById();
		List<Coupon> list = new ArrayList<>();
		if (falg) {
			user = user.selectById();
			if (user.getCount() == 1) {
				Coupon coupon = new Coupon();
				EntityWrapper<Coupon> ew = new EntityWrapper<>();
				ew.eq("type", 2);
				ew.orderBy("stop_time", false);
				List<Coupon> lists = coupon.selectList(ew);
				// 当前时间
				String time = Hutool.parseDateToStringLp();
				int sums = 0;
				for (Coupon coupon2 : lists) {
					// 获取开始时间距今时间
					String times = "";
					if (coupon2.getCreatetime() != null && coupon2.getCreatetime() != "") {
						times = DateUtil.dateDiff(coupon2.getCreatetime(), time, "yyyy-MM-dd", "d");
					}
					// 获取截止时间距今时间
					String dateTime = "";
					if (coupon2.getStopTime() != null) {
						dateTime = DateUtil.dateDiff(coupon2.getStopTime(), time, "yyyy-MM-dd", "d");
					}
					if (!dateTime.equals("0") || Integer.parseInt(dateTime) < 0 && Integer.parseInt(times) > 0) {
						sums = sums + 1;
						if (sums > 3) {
							break;
						}

						Dining dining2 =  dining.selectById(coupon.getDiningId());
						if (dining2 != null) {
							coupon.setDinName(dining2.getDiningName());
						}
						list.add(coupon2);
					} else {
						coupon2.deleteById();
						continue;
					}
				}
			} else if (counId != null && counId != "") {
				String appid[] = counId.split(",");
				for (int i = 0; i < appid.length; i++) {
					String string = appid[i];
					Coupon coupon = new Coupon();
					coupon = coupon.selectById(string);
					if (coupon.getAddtime() != null && coupon.getAddtime() != "") {
						MyCoupon myCoupon = new MyCoupon();
						EntityWrapper<MyCoupon> ew = new EntityWrapper<>();
						ew.eq("user_id", id);
						ew.eq("coupon_id", string);
						myCoupon = myCoupon.selectOne(ew);
						if (myCoupon != null) {
							coupon.setText(1);
						} else {
							coupon.setText(0);
						}
						Dining dining2 =  dining.selectById(coupon.getDiningId());
						if (dining2 != null) {
							coupon.setDinName(dining2.getDiningName());
						}
						list.add(coupon);
					}
				}
			} else {
				Coupon coupon = new Coupon();
				EntityWrapper<Coupon> ew = new EntityWrapper<>();
				ew.eq("type", 1);
				ew.orderBy("stop_time", false);
				List<Coupon> lists = coupon.selectList(ew);
				// 当前时间
				String time = Hutool.parseDateToStringLp();
				int sums = 0;
				for (Coupon coupon2 : lists) {
					// 获取开始时间距今时间
					String times = "";
					if (coupon2.getCreatetime() != null && coupon2.getCreatetime() != "") {
						times = DateUtil.dateDiff(coupon2.getCreatetime(), time, "yyyy-MM-dd", "d");
					}
					// 获取截止时间距今时间
					String dateTime = "";
					if (coupon2.getStopTime() != null && coupon2.getStopTime() != ""
							&& coupon2.getStopTime().length() > 0) {
						dateTime = DateUtil.dateDiff(coupon2.getStopTime(), time, "yyyy-MM-dd", "d");
					}
					if (!dateTime.equals("0") || Integer.parseInt(dateTime) < 0 && Integer.parseInt(times) > 0) {
						MyCoupon myCoupon = new MyCoupon();
						EntityWrapper<MyCoupon> ews = new EntityWrapper<>();
						ews.eq("user_id", id);
						ews.eq("coupon_id", coupon2.getId());
						myCoupon = myCoupon.selectOne(ews);
						if (myCoupon != null) {
							continue;
						} else {
							coupon.setText(0);
						}
						sums = sums + 1;
						if (sums > 3) {
							break;
						}

						Dining dining2 =  dining.selectById(coupon2.getDiningId());
						if (dining2 != null) {
							coupon2.setDinName(dining2.getDiningName());
						}
						list.add(coupon2);
					} else {
						coupon2.deleteById();
						continue;
					}
				}
			}
			if (pv.getPage() != null && pv.getPageSizeYouHuiJuan() != null) {
				PageHelper.startPage(pv.getPage(), pv.getPageSizeYouHuiJuan());
			}
			System.err.println(pv.getPageSizeYouHuiJuan());
			PageResult pr = PageResult.result(list);
			ResultVo rv = new ResultVo();
			rv.setList(pr.getRows());
			rv.setTotal(pr.getTotal());

			if (rv != null) {
				return Body.newInstance(rv);
			}
		}
		return Body.BODY_500;
	}

	@PostMapping("Count")
	public Body Count(String id) {
		User user = new User();
		user.setId(id);
		user = user.selectById();
		if (user != null) {
			user.setCount(user.getCount() + 1);
			if (user.updateById()) {
				return Body.newInstance(user);
			}
			return Body.BODY_500;
		}
		return Body.BODY_500;
	}

	/**
	 * 后台---查询商家优惠劵
	 */
	@PostMapping("addDinCoupon")
	public Body addDinCoupon(String startTime, String stopTime, String dinName, Integer type) {
		EntityWrapper<Dining> ew = new EntityWrapper<>();
		Dining dining = new Dining();
		if (dinName != null && dinName != "") {
			ew.like("dining_name", dinName);
		}
		List<Dining> list = dining.selectList(ew);
		if (list.size() < 0) {
			return Body.BODY_500;
		}
		EntityWrapper<Coupon> ewq = new EntityWrapper<>();
		Coupon coupon = new Coupon();
		if (startTime != null && startTime != "" && stopTime != null && stopTime != "") {
			String addtime = startTime.substring(startTime.indexOf("-0") + 1).trim();
			String addtimes = addtime.substring(addtime.indexOf("0") + 1).trim();
			String shijian = startTime.substring(0, startTime.indexOf("-0") + 1).trim();

			String shijian1 = addtimes.substring(addtimes.indexOf("0") + 1).trim();

			String add = addtimes.substring(0, addtimes.indexOf("-0") + 1).trim();

			startTime = shijian + add + shijian1;

			String addtime1 = stopTime.substring(stopTime.indexOf("-0") + 1).trim();
			String addtimes1 = addtime1.substring(addtime1.indexOf("0") + 1).trim();

			String shijian2 = startTime.substring(0, startTime.indexOf("-0") + 1).trim();

			String shijian12 = addtimes1.substring(addtimes1.indexOf("0") + 1).trim();

			String add1 = addtimes.substring(0, addtimes.indexOf("-0") + 1).trim();

			stopTime = shijian2 + add1 + shijian12;

			ewq.between("createtime", startTime, stopTime);
		} else if (startTime != null && startTime != "") {
			String addtime = startTime.substring(startTime.indexOf("-0") + 1).trim();
			String addtimes = addtime.substring(addtime.indexOf("0") + 1).trim();

			String shijian = startTime.substring(0, startTime.indexOf("-0") + 1).trim();

			String shijian1 = addtimes.substring(addtimes.indexOf("0") + 1).trim();

			String add = addtimes.substring(0, addtimes.indexOf("-0") + 1).trim();

			startTime = shijian + add + shijian1;

			ewq.like("createtime", startTime);
		} else if (stopTime != null && stopTime != "") {
			String addtime = stopTime.substring(stopTime.indexOf("-0") + 1).trim();
			String addtimes = addtime.substring(addtime.indexOf("0") + 1).trim();

			String addtime1 = stopTime.substring(stopTime.indexOf("-0") + 1).trim();
			String addtimes1 = addtime1.substring(addtime1.indexOf("0") + 1).trim();

			String shijian2 = startTime.substring(0, startTime.indexOf("-0") + 1).trim();

			String shijian12 = addtimes1.substring(addtimes1.indexOf("0") + 1).trim();

			String add1 = addtimes.substring(0, addtimes.indexOf("-0") + 1).trim();

			stopTime = shijian2 + add1 + shijian12;

			ewq.like("stop_time", stopTime);
		}

		if (type != null) {
			ewq.eq("type", type);
		}

		List<Coupon> lists = coupon.selectList(ewq);
		if (lists.size() < 0) {
			return Body.BODY_500;
		}
		List<Coupon> addlist = new ArrayList<>();

		for (Dining Dining : list) {
			for (Coupon coupon2 : lists) {
				if (coupon2.getDiningId() != null && coupon2.getDiningId() != "") {
					if (coupon2.getType() == 1) {
						coupon2.setName("满减劵");
					} else if (coupon2.getType() == 2) {
						coupon2.setName("通用劵");
					}
					if (coupon2.getDiningId().equals(Dining.getId())) {
						coupon2.setDinName(Dining.getDiningName());
						addlist.add(coupon2);
					}
					if (coupon2.getDiningId().length() < 0) {
						addlist.add(coupon2);
					}
					continue;
				}
				continue;
			}
		}
		if (addlist.size() > 0) {
			return Body.newInstance(addlist);
		}
		return Body.BODY_500;
	}

	/**
	 * 后台---查询商家优惠劵
	 */
	@PostMapping("queryDin")
	public Body queryDin(String startTime, String stopTime, String dinName, Integer type) {
		EntityWrapper<Dining> ew = new EntityWrapper<>();
		Dining dining = new Dining();
		if (dinName != null && dinName != "") {
			ew.like("dining_name", dinName);
		}
		List<Dining> list = dining.selectList(ew);
		if (list.size() < 0) {
			return Body.BODY_500;
		}
		EntityWrapper<Coupon> ewq = new EntityWrapper<>();
		Coupon coupon = new Coupon();
		if (type != null) {
			ewq.eq("type", type);
		}
		if (startTime != null && startTime != "" && stopTime != null && stopTime != "") {
			ewq.between("createtime", startTime, stopTime);
		} else if (startTime != null && startTime != "") {
			ewq.like("createtime", startTime);
		} else if (stopTime != null && stopTime != "") {
			ewq.like("stopTime", stopTime);
		}
		List<Coupon> lists = coupon.selectList(ewq);
		if (lists.size() < 0) {
			return Body.BODY_500;
		}
		List<Coupon> lista = new ArrayList<>();
		for (Coupon coupon2 : lists) {
			for (Dining dining2 : list) {
				if (dinName != null && dinName != "") {
					if (coupon2.getDiningId() == dining2.getId()) {
						coupon2.setName(dining2.getDiningName());
					}
					if (coupon2.getType() == 1) {
//						coupon2.setText("满减劵");
					}
					lista.add(coupon2);
				} else {
					lista.add(coupon2);
				}
			}
		}
		if (lista.size() > 0) {
			return Body.newInstance(lista);
		}
		return Body.BODY_500;
	}

}
